//
//  HJSellOderHeader.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSellOderHeader.h"
#import "HJHomeModel.h"

@interface HJSellOderHeader()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *mainIamage;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *staute;
@property (weak, nonatomic) IBOutlet UILabel *goodsAndTime;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (weak, nonatomic) IBOutlet UIView *line1;

// 我的出售View
@property (weak, nonatomic) IBOutlet UIView *meSellView;



// 快速出售View
@property (weak, nonatomic) IBOutlet UIView *fastSellView;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyMoney;
@property (weak, nonatomic) IBOutlet UILabel *money;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *content;

@end
@implementation HJSellOderHeader

-(instancetype)init {
    if (self = [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJSellOderHeader" owner:nil options:nil].firstObject;
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    self.moneyTextField.delegate = self;
    self.moneyTextField.inputAccessoryView = [self addToolbar];
    self.titleLabel.font = HJFontSize(mScreenWidth/414 * 15);
    self.moneyLabel.font = HJFontSize(mScreenWidth/414 * 19);
    self.goodsAndTime.font = HJFontSize(mScreenWidth/414 * 13);
    self.reference.font = HJFontSize(mScreenWidth/414 * 13);
    self.money.font = HJFontSize(mScreenWidth/414 * 18);
    self.time.font = HJFontSize(mScreenWidth/414 * 13);
    
    
//    [self animationLineWithBtn:[self viewWithTag:100]];
}
- (UIToolbar *)addToolbar {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mScreenWidth, 35)];
    toolbar.tintColor = HJColorRGB(0);
    toolbar.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space, bar];
    return toolbar;
}
-(void)textFieldDone {
    if ([self.moneyTextField.text isEqualToString:@"￥"]) {
        self.moneyTextField.text = @"";
    }
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [mNotificationCenter postNotificationName:@"textFieldNotification" object:nil userInfo:@{@"text":self.moneyTextField.text}];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.text =[NSString stringWithFormat:@"￥"];
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField.text isEqualToString:@"￥"]) {
        textField.text = @"";
    }
    return [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    [mNotificationCenter postNotificationName:@"textFieldNotification" object:nil userInfo:@{@"text":textField.text}];
}

-(void)setModel:(HJOderModel *)model {
    _model = model;
    [self.mainIamage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",model.shoes.avatar_base_url,model.shoes.avatar_path]] placeholderImage:nil];
    self.titleLabel.text = model.shoes.shoes_name;
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",model.shoes.sell_money];
    self.goodsAndTime.text = [NSString stringWithFormat:@"%@   %@",model.shoes.shoes_article_number,[CommonTools getTimeStrBytimeStamp:model.shoes.created_at]];
    
    self.reference.text = [NSString stringWithFormat:@"参考价：%@",model.shoes.reference_price];
    self.codeLabel.text = model.want_buy.shoes_size;
    self.money.text = [NSString stringWithFormat:@"￥%@",model.want_buy.shoes_cost];
    self.time.text = [CommonTools getTimeStrBytimeSt:model.want_buy.created_at];
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%@码",self.size] forState:UIControlStateNormal];
    
    if (!model.is_check) {
        [self animationLineWithLine:100 btn:[self viewWithTag:100]];
//        [self animationLineWithBtn:];
    }else {
        [self animationLineWithLine:self.currentTag btn:[self viewWithTag:self.currentTag]];
//        [self animationLineWithBtn:];
    }
}

// 我要出售100 快速出售101
- (IBAction)sellBtn:(UIButton *)sender {

    if (!_model.is_check) {
        [[UIViewController currentViewController] showErrorHUDWithError:@"无当前尺码求购。无法快速出售"];
        return;
    }
//    [self animationLineWithBtn:sender];
    [self animationLineWithLine:sender.tag btn:sender];
    
    if (self.block) {
        self.block(sender.tag);
    }
}

-(void)animationLineWithLine:(NSInteger)tag btn:(UIButton *)btn {
    if (tag==100) {
        [UIView animateWithDuration:0.1 animations:^{
            self.meSellView.hidden = NO;
            self.fastSellView.hidden = YES;
            self.line.hidden = NO;
            self.line1.hidden = YES;
        }];
    }else {
        [UIView animateWithDuration:0.1 animations:^{
            self.meSellView.hidden= YES;
            self.fastSellView.hidden = NO;
            self.line.hidden = YES;
            self.line1.hidden = NO;
        }];
    }
    [self animationLineWithBtn:btn];
}
// 滑动线条
-(void)animationLineWithBtn:(UIButton *)btn {
    for (int i = 100; i<=101; i ++) {
        UIButton *view = [self viewWithTag:i];
        view.alpha = 0.5;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            view.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
        });
    }
    btn.alpha = 1;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    });
}
// 码
- (IBAction)codeBtn:(UIButton *)sender {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"codeBtnNotification" object:nil];
}
@end
